package cn.java.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/MainServlet")
public class MainServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //利用request对象获取URL请求传来的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if ("admin".equals(username) && "123".equals(password)) {
            //1、通过转发实现页面的跳转，当前是转发到success.jsp页面
            //注意：在转发的过程中"/"其实代表的是http://服务器名:8080/工程名/
            request.getRequestDispatcher("/pages/success.jsp").forward(request, response);
        } else {
            //2、使用重定向来实现跳转
            //在重定向中"/"代表的是http://localhost:8080
            response.sendRedirect("/pages/fail.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

